package com.hy.study.mapper;

import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;
import java.util.Arrays;

/**
 * @author huyong
 * @date 2024 年 02 月 07 日 16:33: 31
 */
public class TwoShareFriendsMapper extends Mapper<LongWritable, Text, Text, Text> {
    @Override
    protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, Text>.Context context) throws IOException, InterruptedException {
        String line = value.toString();
        if (StringUtils.isBlank(line)) {
            return;
        }

        String[] split = line.split("\t");
        if (split.length < 2) {
            return;
        }

        String f = split[0];
        String[] persons = split[1].split(",");
        // 对好友进行排序
        Arrays.sort(persons);
        for (int i = 0; i < persons.length; i++) {
            for (int j = i + 1; j < persons.length; j++) {
                context.write(new Text(persons[i] + persons[j]), new Text(f));
            }
        }
    }
}
